﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System;

//网络交互类
public class MyNetWork<T>
{
    /// <summary>
    /// 组织url
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    private string organizeUrl(Dictionary<string, object> data)
    {
        string url = NetWorkManager.Instance.GetUrl() + "/" + data.GetValue("a", "default") + "/" + data.GetValue("m", "default");
        data.RemoveValue("m");
        data.RemoveValue("a");
        return url;
    }
    /// <summary>
    /// 组织数据
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    private WWWForm organizeData(Dictionary<string, object> data)
    {
        WWWForm form = new WWWForm();
        foreach (var item in data)
        {
            form.AddField(item.Key,item.Value.ToString());
        }
        return form;
    }
    /// <summary>
    /// 发起请求
    /// </summary>
    /// <param name="mb"></param>
    /// <param name="data"></param>
    /// <param name="cb"></param>
    public void Request(MonoBehaviour mb,Dictionary<string, object> data, TwoDelegate cb)
    {
        mb.StartCoroutine(Post(data, cb));
    }
    [Serializable]
    class RepClass<T2>
    {
        public T2 result;
        public string t;
        public string a;
        public string m;
        public string code;
        public string message;
    }
    /// <summary>
    /// get 方式发起请求
    /// </summary>
    /// <returns></returns>
    //IEnumerator GetText()
    //{
    //    using (UnityWebRequest www = UnityWebRequest.Get("http://119.29.166.152:82/RenXin/index.php/ajaxgateway/index/user_c/getRankList"))
    //    {
    //        yield return www.Send();

    //        if (www.isError)
    //        {
    //            Debug.Log(www.error);
    //        }
    //        else
    //        {
    //            // Show results as text  
    //            Debug.Log(www.downloadHandler.text);

    //            // Or retrieve results as binary data  
    //            byte[] results = www.downloadHandler.data;
    //        }
    //    }
    //}
    /// <summary>
    /// Post方式发起请求
    /// </summary>
    /// <param name="data"></param>
    /// <param name="cb"></param>
    /// <returns></returns>
    IEnumerator Post(Dictionary<string,object> data,TwoDelegate cb = null)
    {
        string url = organizeUrl(data);
        WWWForm form = organizeData(data);
        using (UnityWebRequest www = UnityWebRequest.Post(url, form))
        {
            yield return www.Send();

            if (www.isNetworkError)
            {
                Debug.Log(www.error);
                if(cb != null) cb(true,www.error);
            }
            else
            {
                // Show results as text  
                Debug.Log(www.downloadHandler.text);
                RepClass<T> rep = null;
                try
                {
                    rep = JsonUtility.FromJson<RepClass<T>>(www.downloadHandler.text);
                    // JsonUtility.FromJsonOverwrite(www.downloadHandler.text,rep);
                    if (rep.code != null)
                    {
                        if(cb != null) cb(true, rep.message);
                    }
                    else
                    {
                        if(cb != null) cb(false, rep.result);
                    }
                }
                catch (Exception e)
                {
                    Debug.Log(e.ToString());
                    if(cb != null) cb(true, "JSON错误");
                }               
            }
        }
    }
}
